package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.JazUserAuntDO;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazUserCommentClassifyDao;
import com.jinzhi.jzweb.domain.JazUserCommentClassifyDO;
import com.jinzhi.jzweb.service.JazUserCommentClassifyService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.*;

/**
 * 
 * <pre>
 * 评价分类表
 * </pre>
 * <small> 2024-05-16 15:08:53 | Long</small>
 */
@Service
public class JazUserCommentClassifyServiceImpl extends CoreServiceImpl<JazUserCommentClassifyDao, JazUserCommentClassifyDO> implements JazUserCommentClassifyService {



    /**
     * 分页查询评价分类
     *
     * @param
     * @return
     */
    @Override
    public Page<JazUserCommentClassifyDO> getUserCommentClassifyList(int pageNumber,int pageSize) {
        Page<JazUserCommentClassifyDO> page = new Page<>(
                Optional.of(pageNumber).orElse(1),
                Optional.of(pageSize).orElse(10));
        Wrapper<JazUserCommentClassifyDO> wrapper = new EntityWrapper<JazUserCommentClassifyDO>().orderBy("create_time", true);
        // 查询列表数据
        Page<JazUserCommentClassifyDO> selectPage = selectPage(page, wrapper);
        return selectPage;
    }

    /**
     * 新增评价类型
     *
     * @param jazUserCommentClassify
     * @return
     */
    @Override
    public Result insertUserCommentClassify(JazUserCommentClassifyDO jazUserCommentClassify) {
        if (jazUserCommentClassify==null) {
            return Result.fail();
        }
        List<Integer> arrayList = new ArrayList<>();
        // 查询最大的评价类型
        Wrapper<JazUserCommentClassifyDO> wrapper = new EntityWrapper<JazUserCommentClassifyDO>().orderBy("comment_type", false);
        List<JazUserCommentClassifyDO> list = selectList(wrapper);
        if (!list.isEmpty()) {
            list.stream().forEach(a->{
                arrayList.add(a.getCommentType());
            });
            // 排序
            Collections.sort(arrayList, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2.compareTo(o1); // 降序排列
                }
            });
            jazUserCommentClassify.setCommentType(arrayList.get(0)+1);
        }else {
            jazUserCommentClassify.setCommentType(1);
        }
        boolean insert = insert(jazUserCommentClassify);
        return insert?Result.ok():Result.fail();
    }

    /**
     * 修改评价类型
     *
     * @param jazUserCommentClassify
     * @return
     */
    @Override
    public Result<String> updateUserCommentClassifyById(JazUserCommentClassifyDO jazUserCommentClassify) {
        if (jazUserCommentClassify==null) {
            return Result.fail();
        }
        boolean update = updateById(jazUserCommentClassify);
        return update?Result.ok():Result.fail();
    }

    /**
     * 修改评价类型状态
     *
     * @param jazUserCommentClassify
     * @return
     */
    @Override
    public Result<String> updateUserCommentClassifyStatus(JazUserCommentClassifyDO jazUserCommentClassify) {
        JazUserCommentClassifyDO jazUserCommentClassifyDO = selectById(jazUserCommentClassify.getId());
        if (jazUserCommentClassifyDO==null) {
            return Result.fail();
        }
        jazUserCommentClassifyDO.setStatus(jazUserCommentClassify.getStatus());
        boolean update = updateById(jazUserCommentClassifyDO);
        return update?Result.ok():Result.fail();
    }
}
